# Title     : TODO
# Objective : TODO
# Created by: Administrator
# Created on: 2019/8/12
library(dplyr)
library(optparse)
library(purrr)
library(DescTools)

option_list <- list(
make_option("--i", default = "AllMet1.csv", type = "character", help = "metabolite data file"),
make_option("--g", default = "SampleInfo.csv", type = "character", help = "sample group file"),
make_option("--cp", default = "", type = "character", help = "compare info ")
# make_option("--sc", default = "sample_color.txt", type = "character", help = "sample color file")
)
opt <- parse_args(OptionParser(option_list = option_list))

groups <- unlist(strsplit(opt$cp, "_vs_"))
groups


sampleInfo <- read.csv(opt$g, header = T, stringsAsFactors = F) %>%
    as.data.frame()
head(sampleInfo)

# sampleColDf <- read.table(opt$sc, header = T, sep = "\t", stringsAsFactors = F, comment.char = "") %>%
# select(c("ClassNote", "col"))

groupData <- groups %>%
map_dfr(function(group){
    inGroups <- unlist(strsplit(group, "\\+"))
    sampleInfo %>%
        filter(ClassNote %in% inGroups) %>%
        mutate(ClassNote = group)
})
groupData

write.csv(groupData, "true_SampleInfo.csv")

# colorData <- groups %>%
# map_dfr(function(group){
#     inGroups <- unlist(strsplit(group, "\\+"))
#     color <- ""
#     for (inGroup in inGroups) {
#         inColor <- sampleColDf %>%
#             filter(ClassNote == inGroup) %>%
#             mutate(ClassNote = group) %>%
#             .$col
#         print(inColor)
#         if (color == "") {
#             color <- inColor
#         }else {
#             color <- MixColor(color, inColor, 0.5)
#         }
#     }
#     return(data.frame(ClassNote = group, col = color))
# })
#
# write.csv(colorData, "true_sample_colOdd_Ratio.csv")


